#include "contactuserlist.h"
#include <QEvent>
#include <QWheelEvent>
#include <QScrollBar>
#include "usermgr.h"
#include <QTimer>
#include <QCoreApplication>
#include "grouptipitem.h"
#include <QRandomGenerator>
#include "tcpmgr.h"

ContactUserList::ContactUserList(QWidget *parent)
    :QListWidget(parent)
    ,_load_pending(false)
    ,_add_friend_item(nullptr)
{
    this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    this->viewport()->installEventFilter(this);
    //模拟从数据库或者后端传输过来的数据,进行列表加载
    addContactUserList();
    connect(this,&QListWidget::itemClicked,this,&ContactUserList::slot_item_clicked);
    //链接对端同意认证后通知的信号
    connect(TcpMgr::GetInstance().get(), &TcpMgr::sig_add_auth_friend,this,
            &ContactUserList::slot_add_auth_friend);

    //链接自己点击同意认证后界面刷新
    connect(TcpMgr::GetInstance().get(), &TcpMgr::sig_auth_rsp,this,
            &ContactUserList::slot_auth_rsp);
}

void ContactUserList::ShowRedPoint(bool bshow)
{
    _add_friend_item->showRedPoint(bshow);
}

bool ContactUserList::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == this->viewport()){
        if(event->type() == QEvent::Enter){
            //鼠标悬浮 显示滚动条
            this->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
        }else if(event->type() == QEvent::Leave){
            //鼠标离开 隐藏滚动条
            this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        }
    }
    if(watched == this->viewport()&&event->type() == QEvent::Wheel){
        QWheelEvent* wheelEvent = static_cast<QWheelEvent*>(event);
        int numDegress = wheelEvent->angleDelta().y()/8;
        int numSteps = numDegress/15;
        //设置滚动幅度
        this->verticalScrollBar()->setValue(this->verticalScrollBar()->value()-numSteps);
        //检查是否滚动到底部
        QScrollBar* scrollBar = this->verticalScrollBar();
        int maxScrollValue = scrollBar->maximum();
        int currentValue = scrollBar->value();
        //int pageSize = 10;//每页加载的联系人的数量
        if(maxScrollValue - currentValue <= 0){
            auto b_loaded = UserMgr::GetInstance()->IsLoadConFin();
            if(b_loaded){
                return true;
            }
            if(_load_pending){
                return true;
            }
            _load_pending = true;
            QTimer::singleShot(100,[this](){
                _load_pending = false;
                QCoreApplication::quit();//完成后退出应用程序
            });
            //滚动到底部 加载新的联系人
            qDebug()<<"loading more contact user";
            emit sig_loading_contact_user();
        }
        return true;//停止事件传递
    }
    return QListWidget::eventFilter(watched,event);
}

void ContactUserList::addContactUserList()
{
    auto * groupTip = new GroupTipItem();
    QListWidgetItem *item = new QListWidgetItem;
    item->setSizeHint(groupTip->sizeHint());
    this->addItem(item);
    this->setItemWidget(item, groupTip);
    item->setFlags(item->flags() & ~Qt::ItemIsSelectable);

    _add_friend_item = new ConUserItem();
    _add_friend_item->setObjectName("new_friend_item");
    _add_friend_item->SetInfo(0,tr("新的朋友"),":/res/add_friend.png");
    _add_friend_item->SetItemType(ListItemType::APPLY_FRIEND_ITEM);
    QListWidgetItem *add_item = new QListWidgetItem;
    //qDebug()<<"chat_user_wid sizeHint is " << chat_user_wid->sizeHint();
    add_item->setSizeHint(_add_friend_item->sizeHint());
    this->addItem(add_item);
    this->setItemWidget(add_item, _add_friend_item);
    //默认设置新的朋友申请条目被选中
    this->setCurrentItem(add_item);

    auto * groupCon = new GroupTipItem();
    groupCon->SetGroupTip(tr("联系人"));
    _groupitem = new QListWidgetItem;
    _groupitem->setSizeHint(groupCon->sizeHint());
    this->addItem(_groupitem);
    this->setItemWidget(_groupitem, groupCon);
    _groupitem->setFlags(_groupitem->flags() & ~Qt::ItemIsSelectable);

    //加载后端发送过来的好友列表
    auto con_list = UserMgr::GetInstance()->GetConListPerPage();
    for(auto & con_ele : con_list){
        auto *con_user_wid = new ConUserItem();
        con_user_wid->SetInfo(con_ele->_uid,con_ele->_name, con_ele->_icon);
        QListWidgetItem *item = new QListWidgetItem;
        //qDebug()<<"chat_user_wid sizeHint is " << chat_user_wid->sizeHint();
        item->setSizeHint(con_user_wid->sizeHint());
        this->addItem(item);
        this->setItemWidget(item, con_user_wid);
    }

    UserMgr::GetInstance()->UpdateContactLoadedCount();

    // 模拟列表， 创建QListWidgetItem，并设置自定义的widget
//    for(int i = 0; i < 13; i++){
//        int randomValue = QRandomGenerator::global()->bounded(100); // 生成0到99之间的随机整数
//        int str_i = randomValue%contactuserlist_strs.size();
//        int head_i = randomValue%contactuserlist_heads.size();
//        int name_i = randomValue%contactuserlist_names.size();

//        auto *con_user_wid = new ConUserItem();
//        con_user_wid->SetInfo(0,contactuserlist_names[name_i], contactuserlist_heads[head_i]);
//        QListWidgetItem *item = new QListWidgetItem;
//        //qDebug()<<"chat_user_wid sizeHint is " << chat_user_wid->sizeHint();
//        item->setSizeHint(con_user_wid->sizeHint());
//        this->addItem(item);
//        this->setItemWidget(item, con_user_wid);
//    }
}

void ContactUserList::slot_item_clicked(QListWidgetItem *item)
{
    QWidget* widget = this->itemWidget(item);//获取自定义对象
    if(!widget){
        qDebug()<<"slot item clicked widget is nullptr";
        return;
    }
    //对自定义对象widget进行操作 将item转化为基类ListItemBase
    ListItemBase* customItem = qobject_cast<ListItemBase*>(widget);
    if(!customItem){
        qDebug()<<"slot item clicked widget is nullptr";
        return;
    }
    auto itemType = customItem->GetItemType();
    if(itemType == ListItemType::INVALID_ITEM||itemType == ListItemType::GROUP_TIP_ITEM){
        qDebug()<<"slot invalid item clicked";
        return;
    }
    if(itemType == ListItemType::APPLY_FRIEND_ITEM){
        qDebug()<<"apply friend item clicked";
        emit sig_swith_apply_friend_page();
        return;
    }
    if(itemType == ListItemType::CONTACT_USER_ITEM){
        //创建对话框 提示用户
        qDebug()<<"contact user item clicked";
        auto con_item = qobject_cast<ConUserItem*>(customItem);
        auto user_info = con_item->GetInfo();
        qDebug()<<"准备跳转到好友申请界面";
        //跳转到好友申请界面
        emit sig_swith_friend_info_page(user_info);
        return;
    }
}

void ContactUserList::slot_add_auth_friend(std::shared_ptr<AuthInfo> auth_info)
{
    qDebug() << "slot add auth friend ";
    bool isFriend = UserMgr::GetInstance()->CheckFriendById(auth_info->_uid);
    if(isFriend){
        return;
    }
    // 在 groupitem 之后插入新项
    auto *con_user_wid = new ConUserItem();
    con_user_wid->SetInfo(auth_info);
    QListWidgetItem *item = new QListWidgetItem;
    //qDebug()<<"chat_user_wid sizeHint is " << chat_user_wid->sizeHint();
    item->setSizeHint(con_user_wid->sizeHint());

    // 获取 groupitem 的索引
    int index = this->row(_groupitem);
    // 在 groupitem 之后插入新项
    this->insertItem(index + 1, item);

    this->setItemWidget(item, con_user_wid);

}

void ContactUserList::slot_auth_rsp(std::shared_ptr<AuthRsp> auth_rsp)
{
    qDebug() << "slot auth rsp called";
    bool isFriend = UserMgr::GetInstance()->CheckFriendById(auth_rsp->_uid);
    if(isFriend){
        return;
    }
    // 在 groupitem 之后插入新项
    auto *con_user_wid = new ConUserItem();
    con_user_wid->SetInfo(auth_rsp->_uid ,auth_rsp->_name, auth_rsp->_icon);
    QListWidgetItem *item = new QListWidgetItem;
    //qDebug()<<"chat_user_wid sizeHint is " << chat_user_wid->sizeHint();
    item->setSizeHint(con_user_wid->sizeHint());

    // 获取 groupitem 的索引
    int index = this->row(_groupitem);
    // 在 groupitem 之后插入新项
    this->insertItem(index + 1, item);

    this->setItemWidget(item, con_user_wid);
}

